<template>
  <div class="goodsinfo-container">
    <transition @beforeEnter="beforeEnter" @enter="enter" @after-enter="afterEnter">
      <div class="ball" ref="ball" v-show="ballFlag"></div>
    </transition>
    <!-- 商品轮播图区域 -->
    <div class="mui-card">
      <!-- <swiper-box></swiper-box> -->
      <swiper-box :lunbotuList="lunboList" :isFull="false"></swiper-box>
    </div>
    <!-- 商品购买区域 -->
    <div class="mui-card">
      <div class="mui-card-header">{{ goodsInfolist.title}}</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p class="price">
            市场价：
            <del>￥{{ goodsInfolist.market_price}}</del>&nbsp;&nbsp;销售价：
            <span class="now_price">￥{{goodsInfolist.sell_price}}</span>
          </p>
          <p>
            购买数量：
             <!-- 子组件调用父组件的方法 getCount是子组件中的this.$emit('getCount') getShopNum()是父组件的方法 -->
            <numbox @getCount="getShopNum" :max="goodsInfolist.stock_quantity"></numbox>
          </p>
          <p>
            <mt-button type="primary">点击购买</mt-button>
            <mt-button type="danger" @click="addBallToCar()">加入购物车</mt-button>
          </p>
        </div>
      </div>
    </div>
    <!-- 商品参数区域 -->
    <div class="mui-card">
      <div class="mui-card-header">商品参数</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p>商品货号：{{goodsInfolist.goods_no}}</p>
          <p>库存情况：{{goodsInfolist.stock_quantity}}件</p>
          <p>上架时间：{{goodsInfolist.add_time}}</p>
        </div>
      </div>
      <div class="mui-card-footer">
        <mt-button type="primary" plain size="large" @click="goDesc(id)">图文介绍</mt-button>
        <mt-button type="danger" plain size="large" @click="goComment(id)">商品评论</mt-button>
      </div>
    </div>
  </div>
</template>
<script>
import swiper from "../subcomponents/swiper.vue";
import numbox from "../goodLists/GoodsInfo_numbox.vue";
export default {
  data() {
    return {
      id: this.$route.params.id, //获取参数列表中的id
      lunboList: [
        { img_url: "/src/images/1.jpg" },
        { img_url: "/src/images/2.jpg" },
        { img_url: "/src/images/3.jpg" }
      ], //轮播图数据
      goodsInfolist: {
        add_time: "2019-04-19T17:19:30.000Z",
        goods_no: "SD2932214404",
        id: 89,
        market_price: 2699,
        sell_price: 2199,
        stock_quantity: 60,
        title: "小米（MI）小米note 64G双网通版"
      }, //商品信息
      ballFlag: false ,//小球的显示与隐藏变量
      shopCarNum:1//加入购物车商品的数量
    };
  },
  created() {
    this.getLunbotu();
    this.getGoodsInfo();
  },
  methods: {
    getLunbotu() {
      this.$http.get("api/getthumimages/" + this.id).then(result => {
        if (result.body.status === 0) {
          //先循环遍历轮播图数组的每一项，为item添加img属性，因为轮播图组件中，只认识item.img_url
          //不认识item.src
          result.body.message.forEach(item => {
            item.img = item.src;
          });
          this.lunboList = result.body.message;
        }
      });
    },
    getGoodsInfo() {
      //获取商品的信息
      this.$http.get("api/goods/getinfo/" + this.id).then(reuslt => {
        if (result.body.status === 0) {
          this.goodsInfolist = result.body.message[0];
        }
      });
    },
    goDesc(id) {
      //编程式跳转到新页面
      this.$router.push("/home/goodsDesc/" + id);
    },
    goComment(id) {
      //编程式跳转到新页面
      this.$router.push({ name: "goodsComment", params: { id } });
    },
    addBallToCar() {
      //加入到购物车
      this.ballFlag = !this.ballFlag;
      //需要传入给store一个对象
      //id:商品id，count:购买数量，price：商品单价，selected：是否被选中
      var goodsInfo = {
        id:this.id,
        count:this.shopCarNum,
        price:this.goodsInfolist.sell_price,
        selected:true

      }

      //调用store中mutations中的addToCar方法
      this.$store.commit('addToCar',goodsInfo);
    },
    beforeEnter(el) {
      el.style.transform = "translate(0,0)";
    },
    enter(el, done) {
      el.offsetWidth; //offsetWidth会强制动画刷新
      //获取小球在页面中的位置
      //ref用来获取dom元素    this.$refs.ball    ------------获取ref为ball的元素
      const ballPosition = this.$refs.ball.getBoundingClientRect();
      //使用dom元素获取购物车上标
      const badgePosition = document.getElementById('badge').getBoundingClientRect();
      const xDist = badgePosition.left - ballPosition.left;
      const yDist = badgePosition.top - ballPosition.top;

      el.style.transform = `translate(${xDist}px,${yDist}px)`;
      el.style.transition = "all 0.5s cubic-bezier(.7,.05,.75,.37)";

      done(); //这里的done是afterEnter函数的引用
    },
    afterEnter(el) {
      this.ballFlag = !this.ballFlag;
    },
    getShopNum(count){
     //获取加入购物车的数量
     this.shopCarNum =count;
    },
    
  },
  components: {
    "swiper-box": swiper,
    numbox
  }
};
</script>
<style lang="scss" scoped>
.goodsinfo-container {
  background: #ccc;
  padding-top: 1px;

  .price {
    .now_price {
      color: red;
      font-size: 16px;
      font-weight: bold;
    }
  }

  .mui-card-footer {
    display: block;
    button {
      margin: 10px 0;
    }
  }
  .ball {
    width: 15px;
    height: 15px;
    background: #f00;
    border-radius: 50%;
    position: absolute;
    top: 343px;
    left: 152px;
    z-index: 99;
  }
}
</style>

